﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinalProject.Services.Interfaces;
using FinalProject.Entities;
using FinalProject.Web.ViewModels;
using AutoMapper;

namespace FinalProject.Web.Controllers
{
    public class ClientController : Controller
    {
        private readonly IClientService _clientService;
        private readonly IStoreService _storeService;


        public ClientController(IClientService clientService, IStoreService storeService)
        {
            this._clientService = clientService;
            this._storeService = storeService;
        }

        [Authorize]
        [HttpGet]
        public ActionResult List()
        {
            var list = _clientService.GetAll().Entities;
            List<ClientViewModel> ViewModelList = new List<ClientViewModel>();
            foreach (Client client in list)
            {
                ViewModelList.Add(new ClientViewModel(client));
            }
            if (list.Any())
                return View("List", ViewModelList);
            return View(list.ToList());
        }

        [HttpGet]
        public ActionResult Details(int? Id = 0)
        {
            try
            {
                var ClientTicketViewModel = new ClientTicketViewModel();
                var client = _clientService.GetClientWithTickets((int)Id);
                if (client == null)
                    throw new ArgumentNullException("Client Not Found");
                ClientTicketViewModel.Client = client;
                ClientTicketViewModel.ClientTickets = client.Tickets.OrderByDescending(x => x.Name).ToList();

                return View(ClientTicketViewModel);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "Client", "Details");
                return View("Error", model);
            }
        }

        [HttpGet]
        public ActionResult Edit(int? Id = 0)
        {
            try
            {
                var client = _clientService.GetById((int)Id);
                if (client == null)
                    throw new ArgumentNullException("Client Not Found");
                var clientViewModel = Mapper.Map<Client, ClientViewModel>(client);
                //var client = _clientService.GetById((int)Id);
                //if (client == null)
                //    throw new ArgumentNullException("Client Not Found");
                //ClientViewModel clientViewModel = new ClientViewModel(client);
                clientViewModel.Stores = _storeService.GetAll().Entities.ToList();

                return View(clientViewModel);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "Client", "Edit");
                return View("Error", model);
            }
        }

        [HttpPost]
        public ActionResult Edit(ClientViewModel clientViewModel)
        {

            var client = _clientService.GetById(clientViewModel.Id);

            client = Mapper.Map<ClientViewModel, Client>(clientViewModel);

            //client.PersonName = clientViewModel.PersonName;
            //client.Phone = clientViewModel.Phone;
            //client.LastAccessed = DateTime.Now;
            //client.LastEdited = DateTime.Now;
            //client.StoreId = clientViewModel.StoreId;

            _clientService.Update(client);

            return RedirectToAction("List", "Client");
        }

        [HttpGet]
        public ActionResult Delete(int id = 0)
        {
            try
            {
                var client = _clientService.GetById(id);
                if (client == null)
                    throw new ArgumentNullException("Client Not Found");
                ClientViewModel clientViewModel = new ClientViewModel(client);
                return View(clientViewModel);
            }
            catch(Exception ex)
            {
                var model = new HandleErrorInfo(ex, "Client", "Delete");
                return View("Error", model);
            }
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(ClientViewModel clientViewModel)
        {
            _clientService.Delete(clientViewModel.Id);
            return RedirectToAction("List", "Client");
        }

        [HttpGet]
        public ActionResult Create()
        {
            ClientViewModel clientViewModel = new ClientViewModel();
            clientViewModel.Stores = _storeService.GetAll().Entities.ToList();

            return View(clientViewModel);
        }

        [HttpPost]
        public ActionResult Create(ClientViewModel clientViewModel)
        {
            Client client = new Client();
            client.Created = DateTime.Now;
            client.LastAccessed = DateTime.Now;
            client.LastEdited = DateTime.Now;
            client.PersonName = clientViewModel.PersonName;
            client.Phone = clientViewModel.Phone;
            client.StoreId = clientViewModel.StoreId;

            _clientService.Create(client);

            return RedirectToAction("List", "Client");
        }

    }
}
